﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Axe.Data.Mapping
{
    public class Mapper
    {
        /// <summary>
        /// Получаем значение первичного ключа для данного объекта
        /// </summary>
        /// <param name="classInstance"></param>
        /// <param name="objectType"></param>
        /// <returns></returns>
        public static object GetPrimaryKeyValue(object classInstance, Type objectType) 
        {
            MappingDescription descr = MappingDescriptionBuilder.GetDescription(objectType);
            if (descr.HasPrimaryKey == false)
                throw new InvalidOperationException(Res_Exceptions.MissingPK);

            return descr.PrimaryKey.GetValue(classInstance);
        }

        public static string GetPrimaryKeyFieldName(Type objectType)
        {
            MappingDescription descr = MappingDescriptionBuilder.GetDescription(objectType);
            if (descr.HasPrimaryKey == false)
                throw new InvalidOperationException(Res_Exceptions.MissingPK);

            return descr.PrimaryKey.MappedName;
        }
    }
}
